package com.maddox.relax.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author Maddox
 * RecyclerView悬停分割线
 */
public class HoverItemDecoration extends RecyclerView.ItemDecoration {

    private Context context;

    private int width;

    //分组item的高度
    private int itemHeight;
    //分割线的高度
    private int itemDivideHeight;
    //分组Text距离左边的距离
    private int itemTextPaddingLeft;
    //分组item的画笔
    private Paint itemPaint;
    //分组item的颜色
    private int itemHoverBgColor = 0xF4F4F4;
    //分组item的文字颜色
    private int itemHoverTextColor = 0x999999;
    //悬停item的画笔
    private Paint itemHoverPaint;
    //文字的画笔
    private Paint textPaint;
    //绘制文字的矩形边框
    private Rect textRect = new Rect();
    //分组字母的回调
    private BindItemTextCallback bindItemTextCallback;

    public HoverItemDecoration(Context context) {
        this.context = context;
        width = context.getResources().getDisplayMetrics().widthPixels;
        itemPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        itemPaint.setColor(itemHoverBgColor);
        itemHoverPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        itemHoverPaint.setColor(itemHoverBgColor);
        itemHeight = dp2px(30);
        itemTextPaddingLeft = dp2px(20);
        itemDivideHeight = dp2px(1);
        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(itemHoverTextColor);
        textPaint.setTextSize(dp2px(15));
    }


    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDraw(c, parent, state);
        int count = parent.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = parent.getChildAt(i);
            //分组item的顶部和底部
            int itemTop = child.getTop() - itemHeight;
            int itemBottom = child.getTop();
            //可见item在apdapter中的真是位置
            int position = parent.getChildAdapterPosition(child);
            //获取回调的分组文字
            String text = bindItemTextCallback.getItemText(position);

            //如果是一组中第一个的话绘制出分组的item和文字,否则绘制分割线
            if (isFirstInGroup(position)) {
                c.drawRect(0, itemTop, width, itemBottom, itemPaint);
                drawText(c, itemTop, itemBottom, text);
            }else{
                c.drawRect(0,child.getTop()-itemDivideHeight,width,child.getTop(),itemPaint);

            }
        }


    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);
        if (isFirstInGroup(position)){
            outRect.top=itemHeight;
        }else{
            outRect.top=itemDivideHeight;
        }
    }

    private void drawText(Canvas c, int itemTop, int itemBottom, String text) {
        textRect.left = itemTextPaddingLeft;
        textRect.top = itemTop;
        textRect.bottom = itemBottom;
        textRect.right = text.length();
        Paint.FontMetricsInt fontMetricsInt = textPaint.getFontMetricsInt();
        int baseLine = (textRect.bottom + textRect.top - fontMetricsInt.bottom - fontMetricsInt.top) / 2;
        c.drawText(text, textRect.left, baseLine, textPaint);
    }

    //判断指定位置的item是否是当前分组的第一个
    private boolean isFirstInGroup(int position) {
        if (position == 0) {
            return true;
        } else {
            String preItemText = bindItemTextCallback.getItemText(position - 1);
            String currentItemText = bindItemTextCallback.getItemText(position);
            //比较两个值来判断当前是否与上一个是同一组
            if (preItemText.equals(currentItemText)) {
                return false;
            } else {
                return true;
            }
        }

    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }


    public void setBindItemTextCallback(BindItemTextCallback bindItemTextCallback) {
        this.bindItemTextCallback = bindItemTextCallback;
    }

    public interface BindItemTextCallback {
        String getItemText(int position);
    }


}
